String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function validateRequired(fieldName) {
	var field = $("#" + fieldName);
	if (field.val() != undefined && field.val().trim() == "") {
		$("#" + fieldName + "Group").addClass("error");
		$("#" + fieldName + "Error").show();
		return false;
	} else {
		return true;
	}
}


function validatePositiveInteger(fieldName) {
	
	var field = $("#" + fieldName);
	if (field.val() != undefined && !check(field.val())) {
		$("#" + fieldName + "Group").addClass("error");
		$("#" + fieldName + "Error").show();
		return false;
	} else {
		return true;
	}
}

function check(zahl) { 

	return !isNaN(zahl) && zahl == Math.round(zahl) && zahl > 0;
}

function resetForm() {
	
	$(".help-inline").hide();
	$("#errorMessage").hide();
	$(".control-group").removeClass("error");
}

$(document).ready(function() {

    resetForm();
	$("#detailForm").submit(function() {
        resetForm();
		var valid = true;
		valid = valid & validateRequired("colorName");
		valid = valid & validateRequired("typeName");
		valid = valid & validateRequired("articleNumber");
		valid = valid & validateRequired("seriesName");
		valid = valid & validateRequired("shelfName");
		valid = valid & validatePositiveInteger("shelfNumber");
		valid = valid & validateRequired("rackName");
		valid = valid & validatePositiveInteger("rackNumber");
		valid = valid & validatePositiveInteger("profileCount");
		valid = valid & validatePositiveInteger("profileLength");
		
		if (!valid) {
			$("#errorMessage").show();
			return false;
		}
		return true;
	});
});